package cn.frontop.funswitch.controller;

import cn.frontop.funswitch.common.Result;
import cn.frontop.funswitch.entity.DatasourceEntity;
import cn.frontop.funswitch.entity.IndexConfigEntity;
import cn.frontop.funswitch.entity.ProjectEntity;
import cn.frontop.funswitch.entity.TokenEntity;
import cn.frontop.funswitch.service.DatasourceService;
import cn.frontop.funswitch.service.IndexConfigService;
import cn.frontop.funswitch.service.ProjectService;
import cn.frontop.funswitch.service.TokenService;
import cn.frontop.funswitch.util.CommonUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


/**
 * 项目表
 *
 * @author Yun
 * @email Yun@frontop.cn
 * @date 2022-10-19 15:41:15
 */
@RestController
@RequestMapping("/project")
@Api(tags = "项目管理")
public class ProjectController {
    @Autowired
    private ProjectService projectService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IndexConfigService indexConfigService;
    @Autowired
    private DatasourceService datasourceService;


    /**
     * 查看项目管理列表
     *
     * @param pageNo   当前页码
     * @param pageSize 每页显示条数
     * @return 结果集
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation("查看项目管理列表")
    public Result<?> queryApiPathList(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return CommonUtil.entityPageList(projectService, pageNo, pageSize,new QueryWrapper<ProjectEntity>().lambda().orderByDesc(ProjectEntity::getCreatedTime));

    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation("查询项目信息")
    public Result<ProjectEntity> info(@PathVariable("id") Integer id) {
        ProjectEntity project = projectService.getById(id);
        return Result.ok(project);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("保存项目信息")
    public Result<String> save(@RequestBody ProjectEntity project) {
        projectService.save(project);
        return Result.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("更新项目信息")
    public Result<String> update(@RequestBody ProjectEntity project) {
        project.setUpdatedTime(new Date());
        projectService.updateById(project);
        return Result.ok();
    }

    /**
     * 删除
     */
    @GetMapping("/delete/{id}")
    @ApiOperation("删除项目信息")
    public Result<String> delete(@PathVariable("id") Integer id) {
        //删除项目的同时 会删除其项目下的 所有信息
        projectService.removeById(id);
        tokenService.remove(new QueryWrapper<TokenEntity>().lambda().eq(TokenEntity::getProjectId, id));
        indexConfigService.remove(new QueryWrapper<IndexConfigEntity>().lambda().eq(IndexConfigEntity::getProjectId, id));
        datasourceService.remove(new QueryWrapper<DatasourceEntity>().lambda().eq(DatasourceEntity::getProjectId, id));
        return Result.ok();
    }

}
